# -*- codeing = utf-8 -*-
# @Time : 2020/9/17 13:43
# @Author : 阳某
# @File : 模拟淘宝登录.py
# @Software : PyCharm
# encoding:utf-8
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
import time


class Taobao_login(object):
    def __init__(self):
        self.browser = webdriver.Chrome()
        self.browser.get('https://www.taobao.com')
        self.wait = WebDriverWait(self.browser, 10)
        # 登录操作

    def _put_info(self):
        # 等待密码登录选项出现并跳转登录页面
        password_login = self.wait.until(
            EC.presence_of_element_located((By.XPATH, "//div[@class='site-nav-sign']//a[@class='h']")))
        password_login.click()
        # 登录
        input_user = self.wait.until(
            EC.presence_of_element_located(
                (By.XPATH, "//div[@class='input-plain-wrap input-wrap-loginid ']//input[@class='fm-text']")))
        input_user.send_keys('tb480426989')
        input_password = self.browser.find_element_by_xpath(
            "//div[@class='input-plain-wrap input-wrap-password']//input[@class='fm-text']")
        input_password.send_keys('xxxxx')

    def _get_track(self):
        '''
        获取运动轨迹
        :return: 运动轨迹
        '''
        # 滑动验证
        distance = 258
        track = []
        current = 0
        # mid = distance*3/13
        t = 1
        v = 258
        if current < distance:
            x = v * t
            current = current + x
            track.append(round(x))
        return track

    def _get_slider(self):
        '''
        获取滑块
        :return: 滑块对象
        '''
        slider = self.wait.until(
            EC.element_to_be_clickable(
                (By.XPATH, '//div[@class="scale_text slidetounlock"]//span[@class="nc-lang-cnt"]')))
        return slider

    def _move_to_gap(self, slider, tracks):
        '''
        按照tracks拖动滑块
        :param spider: 滑块
        :param tracks: 轨迹
        :return:
        '''
        ActionChains(self.browser).click_and_hold(slider).perform()
        for i in tracks:
            ActionChains(self.browser).move_by_offset(xoffset=i, yoffset=0).perform()
            time.sleep(1)
            ActionChains(self.browser).release().perform()

    def _login(self):
        # 点击登录
        button = self.wait.until(
            EC.element_to_be_clickable((By.XPATH, "//div[@class='fm-btn']//button[@type='submit']")))
        button.click()
        time.sleep(1)

    def run(self):
        self._put_info()
        time.sleep(1)
        # tracks = self._get_track()
        # slider = self._get_slider()
        # self._move_to_gap(slider,tracks)
        # time.sleep(1)
        # self._login()

if __name__ == '__main__':
    login = Taobao_login()
    login.run()
'''
url = "https://login.taobao.com/member/login.jhtml"
browser.get(url)
browser.maximize_window() # 最大化

# 填写用户名密码
user = '*****'
password = '*******'
time.sleep(8)

iframe = browser.find_element_by_xpath('//div[@class="bokmXvaDlH"]//iframe')
print(iframe)
browser.switch_to.frame(iframe)
browser.find_element_by_xpath('//*[@id="fm-login-id"]').send_keys(id)
browser.find_element_by_xpath('//*[@id="fm-login-password"]').send_keys(password)
time.sleep(2)
# 获取滑块的大小
span_background = browser.find_element_by_xpath('//*[@id="nc_1__scale_text"]/span')
span_background_size = span_background.size
print(span_background_size)

# 获取滑块的位置
button = browser.find_element_by_xpath('//*[@id="nc_1_n1z"]')
button_location = button.location
print(button_location)

# 拖动操作：drag_and_drop_by_offset
# 将滑块的位置由初始位置，右移一个滑动条长度（即为x坐标在滑块位置基础上，加上滑动条的长度，y坐标保持滑块的坐标位置）
x_location = span_background_size["width"]
y_location = button_location["y"]
print(x_location, y_location)
action = ActionChains(browser)
source = browser.find_element_by_xpath('//*[@id="nc_1_n1z"]')
action.click_and_hold(source).perform()
action.move_by_offset(300, 0)
action.release().perform()
time.sleep(1)

# 登录
browser.find_element_by_xpath('//*[@id="login-form"]/div[4]/button').click()
print('登录成功\n')
'''